const express = require("express");
const {uploading} = require("../utils/tools");
const {asyncHandler} = require("../utils/sendResult");
const router = express.Router();
const fs = require("fs")
const path = require("path")
const {ValidationError} = require("../utils/customError");
// 上传文件接口
router.post("/",async (req,res,next)=>{
    const filePath = path.resolve(__dirname, `../public/static/image`)
    try {
        await fs.promises.stat(filePath);
    } catch (e) {
        await fs.promises.mkdir(filePath);
    }
    next()
},uploading.single("file"), asyncHandler( (req, res, next) =>{
    if(!req.file){
        throw new ValidationError("请选择上传的文件!")
    }
    // single 方法里面书写上传控件的 name 值
    return  "/static/image/" + req.file.filename

}))

module.exports = router;
